package client;

import java.io.*;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPClient {
        public static Socket s;
        public static BufferedReader br = null;
        public static BufferedWriter bw = null;
        
        public static Socket init(){
            try {
                String[] str = new String[10];
                str = readF("FileConnect.txt");

                return new Socket(str[0], Integer.parseInt(str[1]));
            } catch (UnknownHostException ex) {
                return null;
            } catch (IOException ex) {
                return null;
            }
        }
        public static int checkConnect(){
            try{
                s=init();
                if(s==null){
                    //s.close();
                    return 0;
                }
                else{
                    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                    
                    bw.write("request");
                    bw.newLine();
                    bw.flush();
                    
                    String receivedMessage;
                    receivedMessage = br.readLine();
                    
                    if(receivedMessage.equals("null")){
                        bw.write("quit");
                        bw.newLine();
                        bw.flush();
                        s.close();
                        bw.close();
                        br.close();
                        return 0;
                    }
                    bw.write("quit");
                    bw.newLine();
                    bw.flush();

                    s.close();
                    bw.close();
                    br.close();
                    return 1;
                }
            }
            catch(IOException ex){
                return 0;
            }
        }

        public static int exitConnect(){
            try{
                s=init();
                if(s==null){
                    //s.close();
                    return 0;
                }
                else{
                    br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                    bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                    String receivedMessage;
                    
                    bw.write("logout");
                    bw.newLine();
                    bw.flush();

                    
                    receivedMessage = br.readLine();

                    bw.write("quit");
                    bw.newLine();
                    bw.flush();

                    s.close();
                    bw.close();
                    br.close();
                    return 1;
                }
            }
            catch(IOException ex){
                return 0;
            }
        }
                
        public static int checkFile(String f){
            File file = new File(f);
            if(file.exists())
                return 1;
            return 0;
        }
        public static int createPort(String n){
             try{
                FileWriter fw;
                int i = 1 ;
                
                String[] str = n.split("/");
                String c=str[0]+"\n";
                while(i<str.length){
                    if((i+1)==str.length)
                        c = c + str[i];
                    else
                        c = c + str[i]+"\n";
                    i=i+1;
                }
                fw = new FileWriter("port.txt");
                fw.write(c);
                fw.flush();
                fw.close();
                return 1;
             }
             catch(IOException ex){
                 return 0;
             }
        }

        public static String[] readF(String file) throws IOException{
            BufferedReader f;
            String[] str = new String[10];
            String str2 = new String();
            int i = 0;
            try{
                f = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
                } catch (FileNotFoundException ex) {
                    return null;
                }
            while (true)
            {
                str2 = f.readLine();
                if(str2 == null)
                    break;
                str[i] = str2;
                i = i+1;
            }
             f.close();

            return str;
         }
       public static int getConnect(String name){
            try{
                s = init();
                if(s==null){
                    return 3;
                }
                br = new BufferedReader(new InputStreamReader(s.getInputStream()));
                bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
                String receivedMessage;

                bw.write("request");
                bw.newLine();
                bw.flush();
                receivedMessage = br.readLine();
                if(receivedMessage.equals("null")){
                    bw.write("quit");
                    bw.newLine();
                    bw.flush();
                    
                    s.close();
                    bw.close();
                    br.close();
                    
                    return 3;
                }
                else{
                    if(createPort(receivedMessage)==0){
                        bw.write("quit");
                        bw.newLine();
                        bw.flush();

                        s.close();
                        bw.close();
                        br.close();
                        return 3;
                    }

                    bw.write("name:"+name);
                    bw.newLine();
                    bw.flush();
                    receivedMessage = br.readLine();
                    //System.out.println(receivedMessage);
                    bw.write("quit");
                    bw.newLine();
                    bw.flush();

                    s.close();
                    bw.close();
                    br.close();
                    return 2;
                }
                
            }
            catch(IOException ex){
                return 3;
            }
        }
       
}
